package cn.virens.mybatis.mapper.example.base;

import java.io.Serializable;
import java.util.Collection;

import cn.virens.common.exception.APIException;

@SuppressWarnings("serial")
public abstract class AbstractExampleCriteria implements Serializable {
	private boolean betweenValue;

	private boolean singleValue;

	private boolean listValue;

	private boolean noValue;

	private String condition;

	private Object value1;

	private Object value2;

	private String andOr;

	public AbstractExampleCriteria(boolean isOr, String condition) {
		this.andOr = isOr ? "OR " : "AND ";
		this.condition = condition;
		this.noValue = true;
	}

	public AbstractExampleCriteria(boolean isOr, String condition, Object value1) {
		if (!(value1 instanceof Collection<?>)) {
			this.andOr = isOr ? "OR " : "AND ";
			this.singleValue = Boolean.TRUE;
			this.condition = condition;
			this.value1 = value1;
		} else {
			this.andOr = isOr ? "OR " : "AND ";
			this.listValue = Boolean.TRUE;
			this.condition = condition;
			this.value1 = value1;
		}
	}

	public AbstractExampleCriteria(boolean isOr, String condition, Object value1, Object value2) {
		this.andOr = isOr ? "OR " : "AND ";
		this.betweenValue = Boolean.TRUE;
		this.condition = condition;
		this.value1 = value1;
		this.value2 = value2;
	}

	public boolean isBetweenValue() {
		return betweenValue;
	}

	public void setBetweenValue(boolean betweenValue) {
		this.betweenValue = betweenValue;
	}

	public boolean isSingleValue() {
		return singleValue;
	}

	public void setSingleValue(boolean singleValue) {
		this.singleValue = singleValue;
	}

	public boolean isListValue() {
		return listValue;
	}

	public void setListValue(boolean listValue) {
		this.listValue = listValue;
	}

	public boolean isNoValue() {
		return noValue;
	}

	public void setNoValue(boolean noValue) {
		this.noValue = noValue;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public Object getValue1() {
		return value1;
	}

	public void setValue1(Object value1) {
		this.value1 = value1;
	}

	public Object getValue2() {
		return value2;
	}

	public void setValue2(Object value2) {
		this.value2 = value2;
	}

	public String getAndOr() {
		return andOr;
	}

	public void setAndOr(String andOr) {
		this.andOr = andOr;
	}

	/** 验证当前条件是否有效 */
	public boolean isVerify() throws APIException {
		return condition != null && condition.isEmpty();
	}
}
